package com.vf.user.service.impl;

import java.util.List;

import com.vf.common.core.domain.entity.SysUser;
import com.vf.system.service.ISysUserService;
import com.vf.user.domain.DTO.UserDTO;
import com.vf.user.domain.DTO.UsersDTO;
import com.vf.user.domain.VO.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vf.user.mapper.UsersMapper;
import com.vf.user.domain.Users;
import com.vf.user.service.IUsersService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户Service业务层处理
 * 
 * @author LBS
 * @date 2025-06-04
 */
@Service
public class UsersServiceImpl implements IUsersService
{
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public Users selectUsersById(Long id)
    {
        return usersMapper.selectUsersById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param users 用户
     * @return 用户
     */
    @Override
    public List<Users> selectUsersList(Users users)
    {
        return usersMapper.selectUsersList(users);
    }

    /**
     * 新增用户
     * 
     * @param users 用户
     * @return 结果
     */
    @Override
    public int insertUsers(Users users)
    {
        return usersMapper.insertUsers(users);
    }

    /**
     * 修改用户
     * 
     * @param users 用户
     * @return 结果
     */
    @Override
    public int updateUsers(Users users)
    {
        return usersMapper.updateUsers(users);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteUsersByIds(Long[] ids)
    {
        return usersMapper.deleteUsersByIds(ids);
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteUsersById(Long id)
    {
        return usersMapper.deleteUsersById(id);
    }

    @Override
    public UsersDTO getUserById(Long userId) {
        return usersMapper.getUserById(userId);
    }

    @Override
    public UserDTO getUserInfo(Long userId) {
        return usersMapper.getUserInfo(userId);
    }

    @Override
    @Transactional
    public int deleteUserById(Long userId) {
        int i = usersMapper.deleteUserById(userId);
        int j = usersMapper.deleteSysUserById(userId);
        if (i > 0 && j > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateUserAvatar(Long userId) {
        return usersMapper.updateUserAvatar(userId);
    }

    @Override
    @Transactional
    public int updateUserInfo(UserVO userVO) {
        int j = usersMapper.updateUserInfo(userVO);
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userVO.getUserId());
        sysUser.setEmail(userVO.getEmail());
        sysUser.setPhonenumber(userVO.getPhone());
        int i = sysUserService.updateUser(sysUser);
        if (i > 0 && j > 0){
            return i;
        }
        return 0;
    }
}
